<script>
  export default {
    onLaunch () {
      // 加载系统信息
      this.$store.dispatch('SystemInfo');
	  document.title = "顺美医疗APS系统";
    },
    onShow () {
    },
    onHide () {
    },
	mounted() {
		const h5js = "https://lf1-cdn-tos.bytegoofy.com/goofy/lark/op/h5-js-sdk-1.5.26.js";
		// this.loadExternalScript(h5js);
	},
	methods: {
		loadExternalScript(url) {
			const script = document.createElement('script');
		    script.src = url;
		    script.onload = () => {
				console.log('SDK JS loaded successfully');
				// SDK 加载完成后可以初始化或使用
				if (window.Lark) {
					// 例如初始化 SDK
					window.Lark.init({
					// 配置参数
					});
				}
				this.init();
		    };
		    script.onerror = (error) => {
				console.error('Error loading SDK:', error);
		    };
		    document.head.appendChild(script);
		},
		init() {
			if (!window.h5sdk) {
				alert("请在飞书中打开该应用");
				return;
			}
			console.log("App.vue初始化方法");
			this.$store.dispatch('Info')
			.then((result) => {
				console.log("通过toekn获取APS系统用户信息", result);
				// if (!result) {
					this.$store.dispatch('FeishuInfo', {url: location.href.split("#")[0]})
					.then((result) => {
						console.log("飞书配置信息结果", result);
						this.feishuSetting(result.data || {});
					}).catch(err => {
						// TODO
						console.log("飞书配置信息发生异常", err);
					}).finally(() => {
						// TODO
					});
				// }
			}).catch(err => {
				// TODO
				console.log("通过toekn获取APS系统用户信息发生异常", err);
			});
		},
		feishuSetting(data) {
			let { appId, noncestr, signature, timestamp } = data;

			try {
				let _this = this;
				window.h5sdk.error((err) => {
					console.log("sdk异常");
					throw ("h5sdk error:", JSON.stringify(err));
				});

				window.h5sdk.config({
					appId: appId,
					timestamp: timestamp,
					nonceStr: noncestr,
					signature: signature,
					jsApiList: ["scanCode"],
					//鉴权成功回调
					onSuccess: (res) => {
					  console.log("SDK鉴权成功");
					  console.log(`config success: ${JSON.stringify(res)}`);
					},
					//鉴权失败回调
					onFail: (err) => {
					  console.log("SDK鉴权报错");
					  throw `config failed: ${JSON.stringify(err)}`;
					},
				  });
				  window.h5sdk.ready(() => {
					tt.requestAuthCode({
						appId: appId,
						success: async (info) => {
						  console.log("获取code成功:", info);
						  _this.$store.dispatch('ApsLogin', { code: info.code})
							.then((result) => {
								uni.$u.toast('飞书鉴权成功');
								console.log("通过code登录APS系统用户信息", result);
								// _this.refreshExtData();
							}).catch(err => {
								uni.$u.toast(`飞书鉴权失败:${err.message}`);
								console.log("通过code登录APS系统用户信息发生异常", err);
							}).finally(() => {
								// TODO
							});
						},
						fail: (error) => {
						  console.error("获取code失败！", error);
						},
					});
				});
			} catch(e){
				//TODO handle the exception
				uni.$u.toast(`FeishuSDK异常:${e.message}`);
				console.error("加载 FeishuSDK 发生异常", e);
			}
		},
		//  TODO 暂时不处理，后续添加权限时使用
		refreshExtData() {
			this.$store.dispatch('UserExtRefresh', {})
			.then((result) => {
				console.log("加载用户扩展数据", result);
			}).catch(err => {
				// TODO
				console.log("加载用户扩展数据发生异常", err);
			}).finally(() => {
				// TODO
			});
		},
	},
  }
</script>

<style lang="scss">
@import "@/uni_modules/uview-ui/index.scss";
@import "@/static/style.scss";
</style>
